
<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="search">
				<el-form-item>
					<el-input v-model="search.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="queryShops">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>


		<!--列表-->
    <el-table :data="pageInfo.list" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="店铺名" width="120">
      </el-table-column>
      <el-table-column prop="tel" label="店铺电话" width="180">
      </el-table-column>
      <el-table-column prop="registerTime" label="注册时间" width="180">
      </el-table-column>
      <el-table-column prop="state" label="店铺状态" width="180">
        <template scope="scope">
          <span v-if="scope.row.state == 1" style="color: purple;">待审核</span>
          <span v-if="scope.row.state == 2" style="color: blue">审核通过待激活</span>
          <span v-if="scope.row.state == 3" style="color: green">激活成功</span>
          <span v-if="scope.row.state == 4" style="color: red">驳回</span>
        </template>

      </el-table-column>





      <el-table-column prop="address" label="地址" width="120">
      </el-table-column>
<!--      <el-table-column prop="logo" label="Logo" min-width="80">-->

<!--      </el-table-column>-->


      <el-table-column label="商店商标" width="120" sortable>
        <template slot-scope="scope">
          <img :src="scope.row.logo" width="120" height="120">
        </template>
      </el-table-column>


      <el-table-column label="操作" width="300">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="primary" size="small" @click="handleReview(scope.$index, scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--
            el-pagination:表示分页
            size-change:每页条数改变的时候出发的方法
            @current-change: 当页数改变的时候
           :current-page: 绑定当前页数据
           :page-sizes="[100, 200, 300, 400]" : 每页多少条的选择
           :page-size="100" 每页的条数
           :total="400" 总条数
           layout="total, sizes, prev, pager, next, jumper"
      -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="search.currentPage"
          :page-sizes="[20, 40, 50, 100]"
          :page-size="100"
          style="float:right;"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total">
      </el-pagination>
		</el-col>

		<!--编辑/新增界面-->
		<el-dialog title="编辑" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="店铺名" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="咨询电话" prop="tel">
          <el-input v-model="saveForm.tel" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="saveForm.state" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="saveForm.address" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="标签" prop="logo">
          <el-input v-model="saveForm.logo" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>



    <!--审核界面-->
    <el-dialog title="审核店铺" :visible.sync="reviewFormVisible" :close-on-click-modal="false">
      <el-form :model="reviewForm" label-width="80px" ref="reviewForm">
<!--        <el-form-item label="店铺名" prop="name">-->
<!--          <el-input v-model="saveForm.name" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="咨询电话" prop="tel">-->
<!--          <el-input v-model="saveForm.tel" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="状态" prop="state">-->
<!--          <el-input v-model="saveForm.state" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="地址" prop="address">-->
<!--          <el-input v-model="saveForm.address" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="标签" prop="logo">-->
<!--          <el-input v-model="saveForm.logo" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item prop="name"label="店铺名">
          <el-input type="text" v-model="reviewForm.name" auto-complete="off" placeholder="请输入店铺名称！"></el-input>
        </el-form-item>
        <el-form-item prop="tel" label="座机">
          <el-input type="text" v-model="reviewForm.tel" auto-complete="off" placeholder="请输入座机！"></el-input>
        </el-form-item>
        <el-form-item prop="address" label="店铺地址">
          <el-input type="text" v-model="reviewForm.address" auto-complete="off" placeholder="请输入地址！"></el-input>
        </el-form-item>
        <el-form-item label="店铺logo">
          <el-image
              style="width: 100px; height: 100px"
              :src="reviewForm.logo">
          </el-image>
        </el-form-item>
        <el-form-item label="驳回原因">
          <el-select v-model="reviewForm.reason" placeholder="请选择原因" clearable>
            <el-option label="店铺名字违规" value="店铺名字违规"></el-option>
            <el-option label="店铺logo违规" value="店铺logo违规"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="reviewForm.content" placeholder="请输入内容" auto-complete="off"></el-input>
        </el-form-item>






      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="reviewFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="activation" :loading="editLoading">通过</el-button>
        <el-button type="danger" @click.native="Reject" :loading="editLoading">驳回</el-button>
      </div>
    </el-dialog>

	</section>
</template>

<script>


	export default {
		data() {
			return {
        departments:[],
        deptTree:[],
        search: {
					keyword: '',
          currentPage:1,
          pageSize:10
				},
        pageInfo:{
          list: [],
          total: 0
        },
				page: 1,
				listLoading: false,
				sels: [],//列表选中列
				saveFormVisible: false,//编辑界面是否显示
        reviewFormVisible: false,
        // excelInputVisible:false,
				editLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				saveForm: {
					id: null,
					name: '',
          tel:'',
          registerTime:'',
          state:'',
          address:'',
          logo:''
				},
        //审核界面数据
        reviewForm:{
          id: null,
          name: '',
          tel:'',
          registerTime:'',
          state:'',
          address:'',
          logo:'',
          reason:'',
          content: ''
          // admin:{
          //   id:null,
          //   username:'',
          //   realName:'',
          //   email:'',
          //   phone:'',
          //   password:'',
          //   state:''
          // }
        },
        uploadVisible: false,
        failVisible: false,
        failList: [],

      }
		},
		methods: {
      // 审核驳回
      Reject() {
        this.$confirm('确认驳回吗?', '提示', {
          type: 'warning'
        }).then(() => {
          //发请求
          this.$http.post("/shop/reject", this.reviewForm)
              .then((result) => {
                result = result.data
                if (result.success) {
                  this.$message({message: '操作成功', type: 'success'});
                  this.listLoading = false;
                  this.reviewFormVisible = false
                  // 刷新列表
                  this.getShops()
                } else {
                  this.$message({message: '操作失败', type: 'danger'});
                }
              })
              .catch((error) => {

              })
        }).catch(() => {

        });
      },
      //通过审核，进入待激活
      activation() {
        console.log(this.reviewForm);
        this.$confirm('确认通过吗?', '提示', {
          type: 'warning'
        }).then(() => {
          //发请求
          this.$http.put("/shop/audit", this.reviewForm)
              .then((result) => {
                result = result.data
                if (result.success) {
                  this.$message({message: '操作成功', type: 'success'});
                  this.listLoading = false;
                  this.reviewFormVisible = false;
                  // 刷新列表
                  this.getShops()
                } else {
                  this.$message({message: '操作失败', type: 'danger'});
                }
              })
              .catch((error) => {
              })
        }).catch(() => {

        });
      },
      // activation: function (index, row) {
      //   console.log(this.reviewForm);
      //   this.$confirm('确认通过该店铺审核吗?', '提示', {
      //     type: 'warning'
      //   }).then(() => {
      //     // var s = JSON.stringify(this.reviewForm);
      //     let para = Object.assign({}, this.reviewForm);
      //     this.$http.put("/shop/audit"+para)
      //         .then(res=>{
      //           res = res.data;
      //           if (res.success){
      //             this.$message({type:"success",message:"店铺审核通过"})
      //           }else {
      //             this.$message.error("店铺审核失败"+res.msg)
      //           }
      //           this.reviewFormVisible = false;
      //           this.search.currentPage= 1;
      //           this.getShops();
      //         })
      //         .catch()
      //   }).catch(() => {
      //
      //   });
      // },


      //显示人工审核界面
      handleReview: function (index, row) {
        this.reviewFormVisible = true;
        this.reviewForm = row;
      },







      beforeAvatarUpload(file) {
        const isXlsx = file.type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        const isSizeLt10M = file.size / 1024 / 1024 < 10;
        if (!isXlsx) {
          this.$message.error('上传文件只能是 xlsx 格式!');
        }
        if (!isSizeLt10M) {
          this.$message.error('上传大小不能超过 10MB!');
        }
        return isXlsx && isSizeLt10M;
      },
      uploadSuccess(response) {
        if (response.success) {
          if (response.data.length) {
            this.failVisible = true;
            this.failList = response.data;
            console.log(this.failList)
          }
          this.$message.success(response.msg)
          this.uploadVisible = false;
        } else {
          this.$message.error(response.msg)
        }
        this.getShops()
      },
      download() {
        this.$http.post("/shop/download", this.search, {responseType: 'blob'})
            .then(response => {
              const fileUrl = URL.createObjectURL(new Blob([response.data]));
              const link = document.createElement('a');
              link.href = fileUrl;
              link.download = "用户信息表" + ".xlsx"; // 设置下载的文件名
              link.click(); // 模拟点击事件，触发文件下载
            })
            .catch(error => this.$message.error('网络异常: ' + error));
      },
      //导出
      downloadTemplate() {
        this.$http.post("/shop/download", [], {responseType: 'blob'})
            .then(response => {
              const fileUrl = URL.createObjectURL(new Blob([response.data]));
              const link = document.createElement('a');
              link.href = fileUrl;
              link.download = "用户信息表" + ".xlsx"; // 设置下载的文件名
              link.click(); // 模拟点击事件，触发文件下载
            })
            .catch(error => this.$message.error('网络异常: ' + error));
      },

		  //查询部门树
      getDeprTree(){
        this.$http.get("/dep/depTree")
            .then(res=>{
              console.log(res);
              res = res.data;
              console.log(res);
              if (res.success){
                this.deptTree=res.data
              }
            })
            .catch(res=>{})
      },
		  //获取所在部门
      getDepartment(){
          this.$http.get("/dep")
            .then(res=>{
              res = res.data;
              console.log(res);
              if (res.success){
                this.departments=res.data
              }
            })
            .catch(res=>{})
      },
		  //点击查询部门
      queryShops(){
        this.search.currentPage=1
        this.getShops()
      },
		  //每页条数变动
      handleSizeChange(val){
        this.search.pageSize = val
        this.getShops()
      },
		  //当前页
			handleCurrentChange(val) {
			  //val指的当前页
        this.search.currentPage=val
				// this.page = val;
				this.getShops();
			},
			//获取员工列表
			getShops() {
        this.listLoading=true;
				this.$http.post("/shop",this.search)
            .then(res=>{
              this.listLoading=false;
              res = res.data;
              console.log(res)
              if(res.success){
                // this.list = res.data.list
                // this.total= res.data.total
                this.pageInfo=res.data
              }
            })
            .catch(res=>{})
			},

			//根据id删除单个数据
			handleDel: function (index, row) {
        console.log(row.id);
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					// let para = { id: row.id };
          this.$http.delete("/shop/"+row.id)
            .then(res=>{
              res = res.data;
              this.listLoading = false;
              if (res.success){
                this.$message({type:"success",message:"删除成功"})
              }else {
                this.$message.error("删除失败"+res.msg)
              }
              this.search.currentPage= 1;
              this.getShops();
            })
            .catch()
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.saveFormVisible = true;
				this.saveForm = row;
				var split = row.path.split("/");
				var data = []
        for (let i = 1; i <split.length-1; i++) {
          data.push(Number.parseInt(split[i]))
        }
        console.log(data);
        this.saveForm.parent = data
        if (!row.parent){
				  this.saveForm.parent={
				    id:null,
				    username:''
          }
        }
        if (!row.manager){
          this.saveForm.manager={
            id:null,
            name:''
          }
        }
        this.getDepartment()
			},

			//显示新增界面
			handleAdd: function () {
				this.saveFormVisible = true;
				this.saveForm = {
          id: null,
          name: '',
          tel:'',
          registerTime:'',
          state:'',
          address:'',
          logo:''
				}
        this.getDepartment()

      },

			//编辑和新增保存信息
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							//NProgress.start();
							let para = Object.assign({}, this.saveForm);


							this.$http.put("/shop",para)
                .then(res=>{
                  this.saveFormVisible=false
                  this.$message({type:"success" ,message:"操作成功"})
                  this.getShops()
                })
                .catch(res=>{})
						});
					}
				});

			},
      inputSubmit(){
        // this.$refs.saveForm.validate((valid) => {
        //   if (valid) {
        //     this.$confirm('确认提交吗？', '提示', {}).then(() => {
        //       //NProgress.start();
        //       let para = Object.assign({}, this.saveForm);
        //
        //       this.$http.put("/shop",para)
        //           .then(res=>{
        //             this.saveFormVisible=false
        //             this.$message({type:"success" ,message:"操作成功"})
        //             this.getShops()
        //           })
        //           .catch(res=>{})
        //     });
        //   }
        // });
      },
			//新增
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
        var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {type: 'warning'})
            .then(() => {
					    this.listLoading = true;
              this.$http.patch("/shop",ids)
                  .then(res=>{
                    this.listLoading = false;
                    res = res.data;
                    if (res.success){
                      this.saveFormVisible=false
                      this.$message({message: '删除成功', type: 'success'});
                    }else {
                      this.saveFormVisible=false
                      this.$message.error("删除失败"+res.msg)
                    }
                    this.search.currentPage=1
                    this.getShops();
                  })
                  .catch(res=>{})
             })
				    .catch(() => {

            });
			}
		},
		mounted() {
			this.getShops();
			this.getDepartment()
		}
	}

</script>

<style scoped>

</style>


